package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.dto.GameInfoDetialV2Bean;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.widgets.DownloadViewStrokeV2Big;

/**
 * Created by Alan on 2018/7/19.
 */

public class AllNetHotGameAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;
    private Map<String, String> downTemp = new HashMap<>();
    private int typeId;

    public AllNetHotGameAdapter(Activity context, List<ViewBean> datas, int typeId) {
        this.mContext = context;
        this.mDatas = datas;
        this.typeId = typeId;
        inflater = LayoutInflater.from(mContext);
    }

    public String getPoByGameId(String gameId) {
        String result = "";
        if (downTemp.size()>0 && downTemp.containsKey(gameId)){
            result = downTemp.get(gameId);
        }
        return result;
    }

    public GameListBaseBean getBeanData(int itemPo, int po) {
        if (mDatas.get(itemPo).getViewType() == ViewType.TYPE_GAME) {
            GameListBaseBean tempJump = (GameListBaseBean) mDatas.get(itemPo).getObj();
            return tempJump;
        }
        return null;
    }

    public void clearTempData () {
        downTemp.clear();
    }

    public enum ViewType {
        TYPE_GAME//游戏信息
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType) {
            this.viewType = viewType;
            this.obj = null;
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_GAME.ordinal()) {
            holder = new GameInfoHolder(inflater.inflate(R.layout.item_media_all_net_hot_game_view, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof GameInfoHolder) {
            final GameListBaseBean temp = (GameListBaseBean) viewBean.getObj();
            GameInfoHolder gameInfoHolder = (GameInfoHolder) holder;

            if (position==0) {
                gameInfoHolder.itemHotGameNumTopTv.setBackgroundResource(R.drawable.all_net_hot_one_bg);
                gameInfoHolder.itemHotGameNumTopTv.setVisibility(View.VISIBLE);
                gameInfoHolder.itemHotGameNumTopTv.setText((position+1)+"");
                gameInfoHolder.itemHotGameNumTv.setVisibility(View.GONE);
            } else if (position == 1) {
                gameInfoHolder.itemHotGameNumTopTv.setBackgroundResource(R.drawable.all_net_hot_two_bg);
                gameInfoHolder.itemHotGameNumTopTv.setVisibility(View.VISIBLE);
                gameInfoHolder.itemHotGameNumTopTv.setText((position+1)+"");
                gameInfoHolder.itemHotGameNumTv.setVisibility(View.GONE);
            } else if (position == 2) {
                gameInfoHolder.itemHotGameNumTopTv.setBackgroundResource(R.drawable.all_net_hot_three_bg);
                gameInfoHolder.itemHotGameNumTopTv.setVisibility(View.VISIBLE);
                gameInfoHolder.itemHotGameNumTopTv.setText((position+1)+"");
                gameInfoHolder.itemHotGameNumTv.setVisibility(View.GONE);
            } else {
                gameInfoHolder.itemHotGameNumTopTv.setVisibility(View.GONE);
                gameInfoHolder.itemHotGameNumTv.setBackgroundResource(R.drawable.all_net_hot_four_bg);
                gameInfoHolder.itemHotGameNumTv.setVisibility(View.VISIBLE);
                gameInfoHolder.itemHotGameNumTv.setText((position+1)+"");
            }

            String icoUrl = temp.getGame_ico();
            if (!StringUtils.isEmpty(icoUrl)) {
                gameInfoHolder.itemHotGameIv.setImageURI(icoUrl);
            }
            gameInfoHolder.itemHotGameNameTx.setText(temp.getGame_name());
            String source = temp.getGame_company_name();
            if (StringUtils.isEmpty(source)) {
                gameInfoHolder.itemHotGameDecTx.setVisibility(View.INVISIBLE);
            } else {
                gameInfoHolder.itemHotGameDecTx.setVisibility(View.VISIBLE);
                gameInfoHolder.itemHotGameDecTx.setText(source);
            }
            if (typeId==1) {
                gameInfoHolder.itemHotGameSoreLl.setVisibility(View.INVISIBLE);
            } else {
                if (temp.getGame_score()!=null) {
                    String score = temp.getGame_score().getScore();
                    if (StringUtils.isEmpty(score) || "0".equals(score)) {
                        gameInfoHolder.itemHotGameSoreLl.setVisibility(View.INVISIBLE);
                    } else {
                        gameInfoHolder.itemHotGameSoreLl.setVisibility(View.VISIBLE);
                        gameInfoHolder.itemHotGameSoreTx.setText(score);
                    }
                } else {
                    gameInfoHolder.itemHotGameSoreLl.setVisibility(View.INVISIBLE);
                }
            }

            downTemp.put(temp.getGame_id(), position+"");
            gameInfoHolder.itemDownloadBtn.initData(temp.getGame_id(), temp);

            gameInfoHolder.itemHotGameRootRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentUtil.getInstance().goGamedetial(mContext, temp.getGame_id());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class GameInfoHolder extends RecyclerView.ViewHolder {
        RelativeLayout itemHotGameRootRl;
        TextView itemHotGameNumTv;
        TextView itemHotGameNumTopTv;
        SimpleDraweeView itemHotGameIv;
        TextView itemHotGameNameTx;
        TextView itemHotGameDecTx;
        TextView itemHotGameSoreTx;
        LinearLayout itemHotGameSoreLl;
        DownloadViewStrokeV2Big itemDownloadBtn;

        public GameInfoHolder(View view) {
            super(view);
            itemHotGameSoreLl = view.findViewById(R.id.itemHotGameSoreLl);
            itemHotGameRootRl = view.findViewById(R.id.itemHotGameRootRl);
            itemHotGameNumTv = view.findViewById(R.id.itemHotGameNumTv);
            itemHotGameNumTopTv = view.findViewById(R.id.itemHotGameNumTopTv);
            itemHotGameIv = view.findViewById(R.id.itemHotGameIv);
            itemHotGameNameTx = view.findViewById(R.id.itemHotGameNameTx);
            itemHotGameDecTx = view.findViewById(R.id.itemHotGameDecTx);
            itemHotGameSoreTx = view.findViewById(R.id.itemHotGameSoreTx);
            itemDownloadBtn = view.findViewById(R.id.itemDownloadBtn);
        }
    }
}
